package mr.ali.videoapp.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import mr.ali.videoapp.portal.model.Collection;
import mr.ali.videoapp.portal.model.CollectionTags;
import mr.ali.videoapp.portal.mapper.CollectionTagsMapper;
import mr.ali.videoapp.portal.service.ICollectionService;
import mr.ali.videoapp.portal.service.ICollectionTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-06-01
 */
@Service
@Slf4j
public class CollectionTagsServiceImpl extends ServiceImpl<CollectionTagsMapper, CollectionTags> implements ICollectionTagsService {
@Resource
private CollectionTagsMapper collectionTagsMapper;

@Resource
private ICollectionService collectionService;

    @Override
    public PageInfo<CollectionTags> videoTags(Integer PageNum, Integer PageSize, Integer SortId , Integer type){
        LambdaQueryWrapper<CollectionTags> wrapper = new LambdaQueryWrapper<>();
        if (type !=0 && type != 5){
            wrapper.eq(CollectionTags ::getType , type);
        }
        wrapper.eq(CollectionTags ::getSortId , SortId);
        wrapper.last("ORDER BY RAND()");
        PageHelper.startPage(PageNum, PageSize);
        List<CollectionTags> tags = collectionTagsMapper.selectList(wrapper);
        for (CollectionTags video : tags) {
            List<Collection> videos = collectionService.videoTagsById(video.getVideoId());
            video.setCollection(videos);
        }
        log.info("VideoTags: {}", tags);

        return new PageInfo<>(tags);
    }
@Override
public PageInfo<CollectionTags> videoSortYearTypeAll(Integer PageNum, Integer PageSize, Integer SortId, Integer type, Integer yurt, String year) {
    LambdaQueryWrapper<CollectionTags> wrapper = new LambdaQueryWrapper<>();
    log.info("pageNum:{},pageSize:{},sortId:{},type:{},yurt:{},year:{}",
            PageNum, PageSize, SortId, type, yurt, year);

    if (type != null && type != 0) {
        wrapper.eq(CollectionTags::getType, type);
    }
    if (yurt != null && yurt != 0) {
        wrapper.eq(CollectionTags::getYurt, yurt);
    }
    if (year != null && !year.isEmpty() && !"null".equals(year) && !"全部".equals(year)) {
        wrapper.eq(CollectionTags::getYear, year);
    }
    if (SortId != null && SortId != 0) {
        wrapper.eq(CollectionTags::getSortId, SortId);
    }

    // 添加随机排序
    wrapper.last("ORDER BY RAND()");

    // 开启分页
    PageHelper.startPage(PageNum, PageSize);

    // 查询原始数据（含重复）
    List<CollectionTags> tags = collectionTagsMapper.selectList(wrapper);

    // Java 层去重
    List<CollectionTags> uniqueTags = tags.stream()
            .collect(Collectors.collectingAndThen(
                    Collectors.toMap(
                            CollectionTags::getVideoId,
                            item -> item,
                            (existing, replacement) -> existing
                    ),
                    map -> new ArrayList<>(map.values())
            ));

    // 补充业务逻辑
    for (CollectionTags video : uniqueTags) {
        List<Collection> videos = collectionService.videoTagsById(video.getVideoId());
        video.setCollection(videos);
    }

    // ✅ 使用 PageHelper 自动封装的 PageInfo（不是你自己 new 的！）
    PageInfo<CollectionTags> pageInfo = new PageInfo<>(tags); // ← PageHelper 提供的构造器
    pageInfo.setList(uniqueTags); // 替换掉 list 为去重后的结果

    return pageInfo;
}
}
